﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using PlayDeezGames.Common.Names;

namespace PlayDeezGames.Common
{
    public class IntGenerator:WeightedGenerator<int>
    {
    }
    public class IntGeneratorProperty : Property
    {
        public class IntGeneratorPropertyPropertyFactory : IPropertyFactory
        {
            public object LoadFromXElement(System.Xml.Linq.XElement theXElement)
            {
                StringGenerator result = new StringGenerator();
                foreach (var entry in theXElement.Elements(Names.CommonXNames.Entry))
                {
                    uint weight = uint.Parse(entry.Element(Names.CommonXNames.Weight).Value);
                    string value = entry.Element(Names.CommonXNames.Value).Value;
                    result[value] = weight;
                }
                return result;
            }

            public void SaveToXElement(System.Xml.Linq.XElement theXElement, object theObject)
            {
                StringGenerator generator = theObject as StringGenerator;
                foreach (var value in generator.Values)
                {
                    theXElement.Add(new XElement(Names.CommonXNames.Entry, new XElement(Names.CommonXNames.Weight, generator[value]), new XElement(Names.CommonXNames.Value, value)));
                }
            }
            private IntGeneratorPropertyPropertyFactory()
            {
            }
            public static void Register()
            {
                PropertyFactories.RegisterFactory(Names.CommonTypes.IntGenerator, new IntGeneratorProperty.IntGeneratorPropertyPropertyFactory(),Names.CommonFactories.IntGeneratorPropertyFactory);
            }
            public static void Unregister()
            {
                PropertyFactories.UnregisterFactory(Names.CommonTypes.IntGenerator);
            }


            public object CreateNew()
            {
                return new IntGenerator();
            }
        }

        public class IntGeneratorPropertyFactory : IEntityFactory
        {
            public IEntity LoadFromXElement(IEntity theParent, XElement theXElement)
            {
                return new IntGeneratorProperty(theParent as Descriptor, theXElement);
            }

            public IEntity Create(IEntity theParent)
            {
                return new IntGeneratorProperty(theParent as Descriptor);
            }
            private IntGeneratorPropertyFactory() { }

            public static void Register()
            {
                EntityFactories.RegisterFactory(Names.CommonFactories.IntGeneratorPropertyFactory, new IntGeneratorProperty.IntGeneratorPropertyFactory());
            }
            public static void Unregister()
            {
                EntityFactories.UnregisterFactory(Names.CommonFactories.IntGeneratorPropertyFactory);
            }
        }

        protected IntGeneratorProperty(Descriptor parent)
            : base(parent)
        {
            Type = CommonTypes.IntGenerator;
        }
        protected IntGeneratorProperty(Descriptor parent, XElement theXElement)
            : base(parent, theXElement)
        {
        }

        protected override XElement OnToXElement()
        {
            XElement result = base.OnToXElement();
            result.Attribute(PlayDeezGames.Common.Names.CommonXNames.Factory).Value = Names.CommonFactories.IntGeneratorPropertyFactory;
            return result;
        }

        public IntGenerator Value
        {
            get
            {
                return Payload as IntGenerator;
            }
            set
            {
                Payload = value;
            }
        }

        public static new IntGeneratorProperty Create(Descriptor theParent)
        {
            return EntityFactories.Create(theParent, Names.CommonFactories.IntGeneratorPropertyFactory) as IntGeneratorProperty;
        }

        public static new IntGeneratorProperty Load(Descriptor theParent, XElement theXElement)
        {
            return EntityFactories.Load(theParent, theXElement) as IntGeneratorProperty;
        }
    }
}
